package com.hz.filemanager.model.file;

import android.os.Environment;
import android.util.Log;


import com.hz.filemanager.R;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class FileUtils {
    //{"ALL","PDF","WORD","EXCEL","PPT","OTHER"};

    /**
     * 文档类型
     */
    public static final int TYPE_DOC = 0;
    public static final int DOC = 10;
    public static final int DOCX = 11;
    public static final int XLS = 12;
    public static final int XLSX = 13;
    public static final int PPT = 14;
    public static final int PPTX = 15;
    public static final int PDF = 16;
    public static final int TXT = 17;

    /**
     * apk类型
     */
    public static final int TYPE_APK = 1;

    /**
     * 压缩包类型
     */
    public static final int TYPE_ZIP = 2;
    public static final int ZIP = 20;
    public static final int RAR = 21;
    public static final int TAR = 22;
    public static final int GZ = 23;


    public static final int TYPE_PDF = 3;
    public static final int TYPE_WORD = 4;
    public static final int TYPE_EXCEL = 5;
    public static final int TYPE_PPT = 6;
    public static final int TYPE_OTHER = 7;
    public static final int TYPE_TXT = 8;
    public static final int TYPE_XLS = 9;


//    /**
//     * 文档文件
//     */
//    public static final int TYPE_TXT = 3;

//    /**
//     * 表格文件
//     */
//    public static final int TYPE_XLS = 4;
//
//    /**
//     * 幻灯片文件
//     */
//    public static final int TYPE_PPT = 5;
//
//    /**
//     * 电子文档
//     */
//    public static final int TYPE_PDF = 6;


    /**
     * 判断文件是否存在
     *
     * @param path 文件的路径
     * @return
     */
    public static boolean isExists(String path) {
        File file = new File(path);
        return file.exists();
    }

    public static boolean isDocWithType(String path, int type){
        boolean result = false;
        path = path.toLowerCase();
        //Log.e("isDocWithType", "  path = " + path);
        if (path.endsWith(".doc") || path.endsWith(".docx") || path.endsWith(".xls") || path.endsWith(".xlsx")
                || path.endsWith(".ppt") || path.endsWith(".pptx") || path.endsWith(".pdf") || path.endsWith(".txt")) {

            if(type == TYPE_DOC){
               return true;
            }
            if(type == TYPE_PDF && path.endsWith(".pdf")){
                return true;
            }
            if(type == TYPE_WORD && (path.endsWith(".doc") || path.endsWith(".docx"))){
                return true;
            }
            if(type == TYPE_EXCEL && (path.endsWith(".xls") || path.endsWith(".xlsx"))){
                return true;
            }
            if(type == TYPE_PPT && (path.endsWith(".pptx"))){
                return true;
            }
            if (type == TYPE_OTHER && (path.endsWith(".txt"))){
                return true;
            }
        }
        //Log.e("isDocWithType", "  result = " + result);
        return result;
    }

    public static int getFileType(String path) {
        path = path.toLowerCase();
        if (path.endsWith(".doc") || path.endsWith(".docx") || path.endsWith(".xls") || path.endsWith(".xlsx")
                || path.endsWith(".ppt") || path.endsWith(".pptx") || path.endsWith(".pdf") || path.endsWith(".txt") ) {
            return TYPE_DOC;
        }else if (path.endsWith(".apk")) {
            return TYPE_APK;
        }else if (path.endsWith(".zip") || path.endsWith(".rar") || path.endsWith(".tar") || path.endsWith(".gz")) {
            return TYPE_ZIP;
        }else{
            return -1;
        }
    }

    public static int getFileTypeOfSort(String path) {
        path = path.toLowerCase();
        if (path.endsWith(".doc") || path.endsWith(".docx")) {
            return TYPE_DOC;
        } else if (path.endsWith(".txt")) {
            return TYPE_TXT;
        } else if (path.endsWith(".pdf")) {
            return TYPE_PDF;
        } else if (path.endsWith(".ppt") || path.endsWith(".pptx")) {
            return TYPE_PPT;
        } else if (path.endsWith(".xls") || path.endsWith(".xlsx")) {
            return TYPE_XLS;
        } else if (path.endsWith(".apk")) {
            return TYPE_APK;
        } else if (path.endsWith(".zip") || path.endsWith(".rar") || path.endsWith(".tar") || path.endsWith(".gz")) {
            return TYPE_ZIP;
        } else {
            return TYPE_OTHER;
        }
    }


    /**
     * 通过文件名获取文件图标
     *
     * @param type 文件类型
     * @return 本地drawable文件类型图片id
     */
    public static int getFileIconByPath(FileTypeEnum type) {
        int iconId = R.mipmap.logo;
        if (type == FileTypeEnum.txt) {
            iconId = R.mipmap.txt;
        } else if (type == FileTypeEnum.doc || type == FileTypeEnum.docx) {
            iconId = R.mipmap.doc;
        } else if (type == FileTypeEnum.xls || type == FileTypeEnum.xlsx) {
            iconId = R.mipmap.xls;
        } else if (type == FileTypeEnum.ppt || type == FileTypeEnum.pptx) {
            iconId = R.mipmap.ppt;
        }
        else if (type == FileTypeEnum.zip || type == FileTypeEnum.rar || type == FileTypeEnum.tar || type == FileTypeEnum.gz) {
            iconId = R.mipmap.zip;
        }
        return iconId;
    }

    public static int getFileIconByPath(File file) {
        int iconId = R.mipmap.logo;
        if (file.isDirectory()) {
            iconId = R.mipmap.logo;//文件图没切出来
        }
        String stuff = com.hz.filemanager.utils.FileUtils.getFileExtension(file.getAbsoluteFile());

        return getFileIconByPath(stuff);
    }

    public static int getFileIconByPath(String path) {
        int iconId = R.mipmap.logo;

        if (path.endsWith("txt")) {
            iconId = R.mipmap.txt;
        } else if (path.endsWith("doc") || path.endsWith("docx")) {
            iconId = R.mipmap.doc;
        } else if (path.endsWith("xls") || path.endsWith("xlsx")) {
            iconId = R.mipmap.xls;
        } else if (path.endsWith("ppt") || path.endsWith("pptx")) {
            iconId = R.mipmap.ppt;
        } else if (path.endsWith("zip") || path.endsWith("rar") || path.endsWith("tar") || path.endsWith("gz")) {
            iconId = R.mipmap.zip;
        }else if(path.endsWith("pdf")){
            iconId = R.mipmap.pdf;
        }else{
            iconId = R.mipmap.icon_splash;
        }
        return iconId;
    }

    /**是否是图片文件*/
    public static boolean isPicFile(String path){
        path = path.toLowerCase();
        if (path.endsWith(".jpg") || path.endsWith(".jpeg") || path.endsWith(".png")){
            return true;
        }
        return false;
    }


    /** 判断SD卡是否挂载 */
    public static boolean isSDCardAvailable() {
        if (Environment.MEDIA_MOUNTED.equals(Environment
                .getExternalStorageState())) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 从文件的全名得到文件的拓展名
     *
     * @param filename
     * @return
     */
    public static String getExtFromFilename(String filename) {
        int dotPosition = filename.lastIndexOf('.');
        if (dotPosition != -1) {
            return filename.substring(dotPosition + 1, filename.length());
        }
        return "";
    }
    /**
     * 读取文件的修改时间
     *
     * @param f
     * @return
     */
    public static String getModifiedTime(File f) {
        Calendar cal = Calendar.getInstance();
        long time = f.lastModified();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        cal.setTimeInMillis(time);
        // System.out.println("修改时间[2] " + formatter.format(cal.getTime()));
        // 输出：修改时间[2] 2009-08-17 10:32:38
        return formatter.format(cal.getTime());
    }


//    public static int getFileStuffType(String stuff) {
//
//        if (stuff.equals("txt")) {
//            return TYPE_TXT;
//        } else if (stuff.equals("doc") || stuff.equals("docx")) {
//            return TYPE_DOC;
//        } else if (stuff.equals("xls") || stuff.equals("xlsx")) {
//            return TYPE_XLS;
//        } else if (stuff.equals("ppt") || stuff.equals("pptx")) {
//            return TYPE_PPT;
//        } else if (stuff.equals("zip") || stuff.equals("rar") || stuff.equals("tar") || stuff.equals("gz")) {
//            return TYPE_ZIP;
//        }
//        return TYPE_OTHER;
//    }
}
